package com.rk.pethome.basic.service.impl;
import com.rk.pethome.basic.mapper.BaseMapper;
import com.rk.pethome.basic.query.BaseQuery;
import com.rk.pethome.basic.service.IBaseService;
import com.rk.pethome.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;
    /**
     * 添加
     *
     * @param t
     */
    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    /**
     * 修改
     *
     * @param t 实体
     */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 根据id删除数据
     *
     * @param id 主键id
     */
    @Override
    @Transactional
    public void delete(Long id) {
       baseMapper.delete(id);
    }

    /**
     * 根据id查询单个对象
     *
     * @param id 主键id
     * @return
     */
    @Override
    @Transactional
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 查询所有的列表
     *
     * @return
     */
    @Override
    @Transactional
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /**
     * 查询分页列表，根据查询条件
     *
     * @param query
     * @return total:总条数
     * result:分页列表
     */
    @Override
    @Transactional
    public PageList<T> loadPagebyQuery(BaseQuery query) {
        Long count = baseMapper.loadCountByQuery(query);
        if(count.equals(0L))
        {
            return new PageList<T>();
        }else {
            List<T> result = baseMapper.loadByQuery(query);
            return  new PageList<T>(count,result);
        }
    }

    /**
     * 批量删除数据
     *
     * @param ts
     */
    @Override
    @Transactional
    public void batchRemove(List<T> ts) {
        baseMapper.batchRemove(ts);
    }
}
